package com.gzdh.dishrecognition.ui.adapter.report;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONArray;
import com.gzdh.dishrecognition.R;

public class DiningTableDetailsAdapter extends RecyclerView.Adapter<DiningTableDetailsAdapter.ViewHolder> {


    private JSONArray jsonArray;
    private Context mContext;


    public DiningTableDetailsAdapter(Context context) {
        mContext = context;
        this.jsonArray = new JSONArray();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dining_table_detail, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if(jsonArray.size() > 0 && position < jsonArray.size()) {
            holder.nameTv.setText(jsonArray.getJSONObject(position).getString("hName"));
            holder.numberTv.setText(jsonArray.getJSONObject(position).getString("number"));
            holder.amountTv.setText(jsonArray.getJSONObject(position).getString("price"));
        }
    }

    public void setJsonArray(JSONArray jsonArray) {
        this.jsonArray = jsonArray;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return jsonArray.size() + 1;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mainLinear;
        TextView nameTv;
        TextView numberTv;
        TextView amountTv;

        ViewHolder(View item) {
            super(item);
            mainLinear = item.findViewById(R.id.mainLinear);
            nameTv = item.findViewById(R.id.nameTv);
            numberTv = item.findViewById(R.id.numberTv);
            amountTv = item.findViewById(R.id.amountTv);
        }

    }
}
